﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HJ212_Common;

namespace HJT212_Protocol_WinServer
{
    public partial class SysConfigBaseForm : Form
    {
        public SysConfigBaseForm()
        {
            InitializeComponent();
            initData();
        }


        #region 字段属性
        private log4net.ILog log = log4net.LogManager.GetLogger("SysConfigBaseForm");

        public ServerMainForm mainForm { get; set; }

        #endregion


        #region 事件
        private void listBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            Brush myBrush = Brushes.Black;
            Color RowBackColorSel = Color.FromArgb(150, 200, 250);//选择项目颜色
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                myBrush = new SolidBrush(RowBackColorSel);
            }
            else
            {
                myBrush = new SolidBrush(Color.White);
            }
            e.Graphics.FillRectangle(myBrush, e.Bounds);
            e.DrawFocusRectangle();//焦点框 


            //绘制图标   
            Image image = imageList1.Images[e.Index];
            Graphics g = e.Graphics;
            Rectangle bounds = e.Bounds;
            Rectangle imageRect = new Rectangle(
                bounds.X,
                bounds.Y,
                bounds.Height,
                bounds.Height);
            Rectangle textRect = new Rectangle(
                imageRect.Right,
                bounds.Y,
                bounds.Width - imageRect.Right,
                bounds.Height);
            if (image != null)
            {
                g.DrawImage(
                    image,
                    imageRect,
                    0,
                    0,
                    image.Width,
                    image.Height,
                    GraphicsUnit.Pixel);
            }
            //文本 
            StringFormat strFormat = new StringFormat();
            //strFormat.Alignment = StringAlignment.Center; 
            strFormat.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(listBoxMenu.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), textRect, strFormat);
        }
        private void listBoxMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBoxMenu.SelectedIndex;

            switch (i)
            {
                case 0:
                    this.grpBoxBase.Visible = true;
                    this.groupBoxEx.Visible = false;
                    break;
                case 1:

                    this.grpBoxBase.Visible = false;
                    this.groupBoxEx.Visible = true;
                    break;
            }
        }
        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 方法


        private void initData()
        {
            try
            {
                string SrvPort = ConfigOprater.GetNodeValue("SrvPort");
                string HostSrvPort = ConfigOprater.GetNodeValue("HostSrvPort");
                string MaxConnNum = ConfigOprater.GetNodeValue("MaxConnNum");
                string LoginTimeOut = ConfigOprater.GetNodeValue("LoginTimeOut");
                string WaitTimeOut = ConfigOprater.GetNodeValue("WaitTimeOut");
                string RetryCount = ConfigOprater.GetNodeValue("RetryCount");
                string IsMnCheck = ConfigOprater.GetNodeValue("IsMnCheck");
                string IsCRCCheck = ConfigOprater.GetNodeValue("IsCRCCheck");
                this.upListenPort.Value = Decimal.Parse(HostSrvPort);
                this.downListenPort.Value = Decimal.Parse(SrvPort);
                this.MaxNum.Value = Decimal.Parse(MaxConnNum);
                this.txtRetryCount.Value = Decimal.Parse(RetryCount);
                this.txtWaitTimeOut.Value = Decimal.Parse(WaitTimeOut);
                this.txtTimout.Value = Decimal.Parse(LoginTimeOut);
                this.IschkLogin.Checked = int.Parse(string.Format("{0}", IsMnCheck)) == 1;
                this.IsCRCChk.Checked = int.Parse(string.Format("{0}", IsCRCCheck)) == 1;
            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }


        }

        #endregion

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr =
                    MessageBox.Show("修改设置之后将重新启动服务\r\n现在应用修改并重启服务吗？",
                    "提示", MessageBoxButtons.OKCancel);

                if (dr != DialogResult.OK) return;

                #region 修改配置文件
                string SrvPort = string.Format("{0}", this.downListenPort.Value);
                string HostSrvPort = string.Format("{0}", this.upListenPort.Value);
                string MaxConnNum = string.Format("{0}", this.MaxNum.Value);
                string LoginTimeOut = string.Format("{0}", this.txtTimout.Value);
                string WaitTimeOut = string.Format("{0}", this.txtWaitTimeOut.Value);
                string RetryCount = string.Format("{0}", this.txtRetryCount.Value);
                bool IschkLogin = this.IschkLogin.Checked;
                bool IsCRCCheck = this.IsCRCChk.Checked;
                ConfigOprater.ModifyKeysValue("SrvPort", SrvPort);
                ConfigOprater.ModifyKeysValue("HostSrvPort", HostSrvPort);
                ConfigOprater.ModifyKeysValue("MaxConnNum", MaxConnNum);
                ConfigOprater.ModifyKeysValue("LoginTimeOut", LoginTimeOut);
                ConfigOprater.ModifyKeysValue("WaitTimeOut", WaitTimeOut);
                ConfigOprater.ModifyKeysValue("RetryCount", RetryCount);
                ConfigOprater.ModifyKeysValue("IsMnCheck", IschkLogin ? "1" : "0");
                ConfigOprater.ModifyKeysValue("IsCRCCheck", IsCRCCheck ? "1" : "0");
                #endregion

                #region 重启服务
                this.Enabled = false;
                mainForm.RestartSrv();
                #endregion
                this.Close();

            }
            catch (Exception ex)
            {
                log.Error("出现异常", ex);
            }
        }
    }
}
